<?php



/**
 * @Author:      lyt
 * @DateTime:    2016-05-03 10:24:34
 * @Description: 用于库存盘点查询的操作
 */
class ScsDataInventoryController extends BaseController
{
    /**
     * @Author:      lyt
     * @DateTime:    2016-05-03 10:57:12
     * @Description: 用于 库存盘点的记录
     */
    public function lists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "page"=>'integer',
                "page_size"=>'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $msl=MaintenanceInventoryList::leftJoin("sc_user",'sc_user.id','=','maintenance_inventory_list.cust_id')->
            leftJoin("maintenance_datebase",'maintenance_datebase.id','=','maintenance_inventory_list.date_id')->where("maintenance_inventory_list.shop_id",$sc_user->shop_id)->orderBy("maintenance_inventory_list.date",'desc');

            if(Input::get("status")!=""){
                $msl=$msl->whereIn("maintenance_inventory_list.status",explode(',',Input::get("status")));
            }
            if(Input::get("date")!=""){
                $msl=$msl->where("maintenance_inventory_list.date",Input::get("date"));
            }
            if(Input::get("list_id")!=""){
                $msl=$msl->where("maintenance_inventory_list.id",Input::get("list_id"));
            }
            if(Input::get('implement_time')!=""){
                $it=Input::get('implement_time');
                $start_time=$it.' 00:00:00';
                $end_time=$it.' 23:59:59';
                $start_time=date("Y-m-d H:i:s",strtotime($start_time)-Config::get("config.report_time_diff"));
                $end_time=date("Y-m-d H:i:s",strtotime($end_time)-Config::get("config.report_time_diff"));
                $msl=$msl->whereBetween("maintenance_inventory_list.implement_time",array($start_time,$end_time));
            }
            $count=$msl->count();
            $msl=$msl->skip($page_size*($page-1))->take($page_size)->select("maintenance_inventory_list.*","maintenance_datebase.name as data_name","sc_user.real_name")->get();
            foreach ($msl as $_msl) {
                if($_msl->implement_time){
                    $_msl->implement_time=date("Y-m-d H:i:s",strtotime($_msl->implement_time)+Config::get("config.report_time_diff"));
                }                
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"inventory_info"=>$msl));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-03 18:46:42
     * @Description: 添加门店的 库存盘点记录
     */
    public function create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'date'=>"required|date",
                'date_id'=>"required",
                'cust_id'=>"required",
                'desc'=>"max:100",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mil=MaintenanceInventoryList::where("shop_id",$sc_user->shop_id)->where("date_id",Input::get("date_id"))->whereIn("status",array(0,1))->first();
            if($mil){//说明还有没完成的 库存盘点表
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $mil=MaintenanceInventoryList::where("shop_id",$sc_user->shop_id)->where("date_id",Input::get("date_id"))->where("status",2)->where("date",">",Input::get("date"))->first();
            if($mil){//说明日期之后的盘点表已经生成
                $response = Lang::get("scs_error.data_inventory_isalready_61001");
                break;
            }
            if(strtotime(Input::get("date"))>strtotime(date("Y-m-d",time()))){//说明日期错误
                $response = Lang::get("scs_error.data_inventory_date_error_61002");
                break;
            }
            $date=Input::get('date');
            $date_id=Input::get('date_id');
            $cust_id=Input::get('cust_id');
            $desc=Input::get('desc');
            $id=Common::createId();
            $ic_id=Input::get('ic_id');
            $ic_ids=isset($ic_id) ? json_decode($ic_id,1) : array();
            $response=DB::transaction(function () use ($date,$date_id,$cust_id,$desc,$id,$sc_user,$ic_ids) {
                $mil=new MaintenanceInventoryList;
                $mil->id=$id;
                $mil->shop_id=$sc_user->shop_id;
                $mil->date=$date;
                $mil->date_id=$date_id;
                $mil->status=0;
                $mil->cust_id=$cust_id;
                $mil->desc=$desc;
                $mil->create_by=$sc_user->id;
                $mil->save();
                //添加盘点包与盘点表关系以及添加分类关系
                if(sizeof($ic_ids)>0){
                    foreach($ic_ids as $ic_id){
                        $ilc=new InventoryListCondition;
                        $ilc->list_id=$id;
                        $ilc->ic_id=$ic_id;
                        $ilc->save();
                        $ic=InventoryCondition::find($ic_id);
                        $ici=InventoryConditionItem::where('ic_id',$ic_id)->where('status',1)->get();
                        foreach($ici as $_ici){
                            $ici_num=InventoryClassification::where('list_id',$id)->where('scf_id',$_ici->scf_id)->count();
                            if($ici_num==0){
                                if($ic->dimension==1){
                                    $icf=new InventoryClassification;
                                    $icf->list_id=$id;
                                    $icf->scf_id=$_ici->scf_id;
                                    $icf->level=1;
                                    $icf->save();
                                }else if($ic->dimension==2){
                                    $icf=new InventoryClassification;
                                    $icf->list_id=$id;
                                    $icf->scf_id=$_ici->scf_id;
                                    $icf->level=2;
                                    $icf->save();
                                }else if($ic->dimension==3){
                                    $icf=new InventoryClassification;
                                    $icf->list_id=$id;
                                    $icf->scf_id=$_ici->scf_id;
                                    $icf->level=3;
                                    $icf->save();
                                }
                            }
                        }
                        //盘点包范围去重如选择二级分类和三级分类两个包，三级分类属于二级分类中，则去掉三级分类获取范围较大的二级分类
                        //三级分类去重
                        $icf3=InventoryClassification::where('list_id',$id)->where('level',3)->select('id','scf_id')->get();
                        $icf2=InventoryClassification::where('list_id',$id)->where('level',2)->select('scf_id')->get()->toArray();
                        $icf1=InventoryClassification::where('list_id',$id)->where('level',1)->select('scf_id')->get()->toArray();
                        $icf2=array_column($icf2,'scf_id');
                        $icf1=array_column($icf1,'scf_id');
                        foreach($icf3 as $_icf3){
                            $st=SupplyClassification::find($_icf3->scf_id);
                            if(in_array($st->parent_id,$icf2)){
                                InventoryClassification::where('id',$_icf3->id)->delete();
                            }else{
                                $st1=SupplyClassification::find($st->parent_id);
                                if(in_array($st1->parent_id,$icf1)){
                                    InventoryClassification::where('id',$_icf3->id)->delete();
                                }
                            }
                        }
                        //二级分类去重
                        $icf2=InventoryClassification::where('list_id',$id)->where('level',2)->select('id','scf_id')->get();
                        $icf1=InventoryClassification::where('list_id',$id)->where('level',1)->select('scf_id')->get()->toArray();
                        
                        foreach($icf2 as $_icf2){
                            $st=SupplyClassification::find($_icf2->scf_id);
                            if(in_array($st->parent_id,$icf1)){
                                InventoryClassification::where('id',$_icf2->id)->delete();
                            }
                        }
                    }
                }
                return  array('code' => 0, 'msg' => 'ok', 'result' => array('id'=>$id));
            });
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-04 09:48:31
     * @Description: 获取一个库存盘点记录的盘点表记录
     *  如果这个库存盘点记录是 新建的  返回结构包括两个部分一部分是已经盘点的 耗材 一部分是还未盘点的
     *  如果是其他状态的 返回 都是已经盘点的记录
     */
    public function getSupplyList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mil=MaintenanceInventoryList::where("id",Input::get("list_id"))->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mil)){
                $response = Lang::get("scs_error.data_inventory_notexit_61003");
                break;
            }
            $search_retrieval_code=Input::get("retrieval_code");
            $level_type=Input::get("level_type",0);
            $level_code=Input::get("level_code",0);
            $three=SupplyItem::getThreeClassification(Input::get('list_id'));
           

            $car_str=Input::get("car_str");
            // $non_ids=SupplyItemNonStandard::retrievalCodeNew($sc_user->shop_id,$search_retrieval_code,$car_str,$level_type,$level_code,$three);//获得非标的耗材id范围
            if($search_retrieval_code||$car_str||($level_type!=0&&$level_code!=0)||sizeof($three)>0){
                $si_ids=SupplyItem::retrievalCodeNew($search_retrieval_code,$car_str,$level_type,$level_code,$three);//获得标准的耗材id范围
            }
           
            
            //已经 盘点过的耗材记录
            if(isset($si_ids)){
                $si_ids[]=0;
            }
            // $sins_sql="select sins.id,1 type from supply_item_non_standard sins ";
            // if(isset($non_ids)){
            //     $non_ids[]=0;
            //     $sins_sql.=" where  sins.id in (".implode(",",$non_ids).")";
            // }
            $item_id=$non_item_id=array();
            $count_sql="SELECT count(1) count FROM `maintenance_inventory_supply` mis
                    where mis.list_id=?  ";
            if(isset($si_ids)){
                $count_sql.=" and mis.item_id in (".implode(",",$si_ids).")";
            }
            $count=DB::select($count_sql,array($mil->id));
            $sql="SELECT mis.* FROM `maintenance_inventory_supply` mis
                    where mis.list_id=? ";
            if(isset($si_ids)){
                $sql.=" and mis.item_id in (".implode(",",$si_ids).")";
            }
            $mis=DB::select($sql." limit ?,?",array($mil->id,$page_size*($page-1),$page_size));

            
            foreach ($mis as $_mis) {
                // if($_mis->item_type==0){//说明是标准耗材
                    $item_id[]=$_mis->item_id;
                    $si_info=SupplyItem::getCacheItemInfo($_mis->item_id);
                    $scf=SupplyClassification::getClassification($si_info->st_classifi_id);
                    $_mis->code=$si_info->code;
                    $_mis->old_code=$si_info->old_code;
                    $_mis->brand_name=$si_info->brand_name;
                    $_mis->series_name=$si_info->series_name;
                    $_mis->model=$si_info->model;
                    $_mis->specification=$si_info->specification;
                    $_mis->scf_id=$si_info->st_classifi_id;
                    $_mis->scf_name=$si_info->scf_name;
                    $_mis->unit=$si_info->unit;
                    $_mis->two_id=$scf->two_id;
                    $_mis->two=$scf->two;
                    $_mis->one_id=$scf->one_id;
                    $_mis->one=$scf->one;
                    $_mis->car_model_comments=$si_info->car_model_comments;
                    $sl=SupplyLocation::where('item_id',$_mis->item_id)->where('shop_id',$sc_user->shop_id)->first();
                    $_mis->location=isset($sl->location) ? $sl->location : '';
                    $sql="select item_type,item_id,sum(current_number*tax_purchase_price) amount from maintenance_storage_item msi
                        where shop_id=? and item_id=? and status=1 and type=1 and data_id=".$mil->date_id;
                    $item=DB::select($sql,array($sc_user->shop_id,$_mis->item_id));
                    $_mis->amount=bcdiv($item[0]->amount,100,2);
                // }else{
                //     $non_item_id[]=$_mis->item_id;
                //     $sins_info=SupplyItemNonStandard::getCacheItemInfo($_mis->item_id);
                //     $scf=StatisticalClassification::getClassification($sins_info->st_classifi_id);
                //     $_mis->code=$sins_info->code;
                //     $_mis->old_code=$sins_info->old_code;
                //     $_mis->brand_name=$sins_info->brand_name;
                //     $_mis->series_name=$sins_info->series_name;
                //     $_mis->model=$sins_info->model;
                //     $_mis->specification=$sins_info->specification;
                //     $_mis->scf_id=$sins_info->st_classifi_id;
                //     $_mis->scf_name=$sins_info->scf_name;
                //     $_mis->unit=$sins_info->unit;
                //     $_mis->two_id=$scf->two_id;
                //     $_mis->two=$scf->two;
                //     $_mis->one_id=$scf->one_id;
                //     $_mis->one=$scf->one;
                //     $_mis->car_model=$sins_info->car_model;
                //     $_mis->car_model_comments=$sins_info->car_model;
                //     $sl=SupplyLocation::where('item_id',$_mis->item_id)->where('item_type',1)->where('shop_id',$sins_info->shop_id)->first();
                //     $_mis->location=isset($sl->location) ? $sl->location : '';
                //     $sql="select item_type,item_id,sum(current_number*tax_purchase_price) amount from maintenance_storage_item msi
                //         where shop_id=? and item_id=? and item_type=? and status=1 and type=1 and data_id=".$mil->date_id;
                //     $item=DB::select($sql,array($sc_user->shop_id,$_mis->item_id,1));
                //     $_mis->amount=bcdiv($item[0]->amount,100,2);
                // }
                $sc=SCUser::find($_mis->create_by);
                $_mis->cust_name=$sc->real_name;
                $_mis->item_name=$si_info->name;
                
            }
            $total_amount=DB::select("SELECT sum(mis.number*msi.tax_purchase_price) amount FROM `maintenance_inventory_storage` mis  LEFT JOIN maintenance_storage_item msi on msi.id=mis.storage_id where mis.list_id=".$mil->id);
            $check_info=array("count"=>$count[0]->count,"total_amount"=>$total_amount[0]->amount,"item"=>$mis);

            
            /*
            *处理查询的
            */
            $close_id=array();//关闭的耗材并且在库存已经为0的不加入盘点
            $close_sql="select mw.item_id from (select item_id from maintenance_warehouse where shop_id=? and number=0) mw LEFT JOIN supply_item si on si.id=mw.item_id where si.`status`=0";
            $close_item=DB::select($close_sql,array($sc_user->shop_id));
            foreach ($close_item as $v) {
                $close_id[]=$v->item_id;
            }
            $all_item=DB::select("select item_id from maintenance_warehouse where shop_id=?",array($sc_user->shop_id));
            $all_id=array();//门店 入国库的耗材
            foreach ($all_item as $v) {
                $all_id[]=$v->item_id;
            }
            $all_id=array_diff($all_id, $close_id);//去除关闭的门店所有入过库的耗材

            $search_array=array();
            if($mil->status==0){//如果没有确认的 库存盘点记录
                $si_sql="";
                if(isset($si_ids)){
                    $si_ids=array_intersect($si_ids,$all_id);//过滤数组和 门店的有效耗材数组交集
                    $temp_ids=array_diff($si_ids, $item_id);
                    $temp_ids[]=0;
                    $si_sql.=" and msi.item_id in (".implode(",",$temp_ids).")";
                }else{
                    $item_id=array_diff($all_id, $item_id);
                    $item_id[]=0;
                    $si_sql.=" and msi.item_id in (".implode(",",$item_id).")";
                }
                // $sins_sql="select sins.id,1 type from supply_item_non_standard sins ";
                // if(isset($non_ids)){
                //     $temp_ids=array_diff($non_ids, $non_item_id);
                //     $temp_ids[]=0;
                //     $sins_sql.=" where  sins.id in (".implode(",",$temp_ids).")";
                // }else{
                //     $non_item_id[]=0;
                //     $sins_sql.=" where  sins.id not in (".implode(",",$non_item_id).")";
                // }
                $sql="select msi.item_type,msi.item_id,sum(msi.current_number*msi.tax_purchase_price) amount,sum(msi.current_number) number ,
                    temp.location from maintenance_storage_item msi
                    LEFT JOIN supply_location  temp on temp.item_id=msi.item_id and  temp.shop_id=?
                    where msi.shop_id=? and msi.status=1 and msi.type=1  ";
                if($si_sql!=""){
                    $sql.=$si_sql;
                }
                $s_item=DB::select($sql."  and msi.data_id=?  group by msi.item_id limit 10000",array($sc_user->shop_id,$sc_user->shop_id,$mil->date_id));
                foreach ($s_item as $_t) {
                    // if($_t->item_type==0){//说明是标准耗材
                        $si_info=SupplyItem::getCacheItemInfo($_t->item_id);
                        $scf=SupplyClassification::getClassification($si_info->st_classifi_id);
                        $_t->code=$si_info->code;
                        $_t->old_code=$si_info->old_code;
                        $_t->brand_name=$si_info->brand_name;
                        $_t->series_name=$si_info->series_name;
                        $_t->model=$si_info->model;
                        $_t->specification=$si_info->specification;
                        $_t->scf_id=$si_info->st_classifi_id;
                        $_t->scf_name=$si_info->scf_name;
                        $_t->unit=$si_info->unit;
                        $_t->two_id=$scf->two_id;
                        $_t->two=$scf->two;
                        $_t->one_id=$scf->one_id;
                        $_t->one=$scf->one;
                        $_t->car_model_comments=$si_info->car_model_comments;
                        $_t->amount= bcdiv($_t->amount,100,2);
                        // $sl=SupplyLocation::where('item_id',$_t->item_id)->where('shop_id',$sc_user->shop_id)->first();
                        // $_t->location=isset($sl->location) ? $sl->location : '';
                        $_t->item_name=$si_info->name;
                    // }else{
                    //     $sins_info=SupplyItemNonStandard::getCacheItemInfo($_t->item_id);
                    //     $scf=StatisticalClassification::getClassification($sins_info->st_classifi_id);
                    //     $_t->code=$sins_info->code;
                    //     $_t->old_code=$sins_info->old_code;
                    //     $_t->brand_name=$sins_info->brand_name;
                    //     $_t->series_name=$sins_info->series_name;
                    //     $_t->model=$sins_info->model;
                    //     $_t->specification=$sins_info->specification;
                    //     $_t->scf_id=$sins_info->st_classifi_id;
                    //     $_t->scf_name=$sins_info->scf_name;
                    //     $_t->unit=$sins_info->unit;
                    //     $_t->two_id=$scf->two_id;
                    //     $_t->two=$scf->two;
                    //     $_t->one_id=$scf->one_id;
                    //     $_t->one=$scf->one;
                    //     $_t->car_model=$sins_info->car_model;
                    //     $_t->car_model_comments=$sins_info->car_model;
                    //     $_t->amount= bcdiv($_t->amount,100,2);
                    //     $sl=SupplyLocation::where('item_id',$_t->item_id)->where('item_type',1)->where('shop_id',$sins_info->shop_id)->first();
                    //     $_t->location=isset($sl->location) ? $sl->location : '';
                        
                    // }
                    $search_array[]=$_t;
                }

                unset($s_item);
                
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("check_info"=>$check_info,"search_info"=>$search_array));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getSupplyList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-04 18:50:58
     * @Description: 添加一个耗材的 盘点表记录
     */
    public function  addSupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
                // 'item_type'=>"required",
                'item_id'=>"required",
                'number'=>"required|min:0",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mil=MaintenanceInventoryList::where("id",Input::get("list_id"))->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mil)){
                $response = Lang::get("scs_error.data_inventory_notexit_61003");
                break;
            }
            if($mil->status!=0){//如果该改库存盘点记录已经确认 或者执行
                $response = Lang::get("scs_error.data_inventory_status_error_61004");
                break;
            }
            $mis=MaintenanceInventorySupply::where("list_id",$mil->id)->where("item_id",Input::get("item_id"))->first();
            if($mis){//如果这个耗材在这个 库存盘点记录里存在  则报错
                $response = Lang::get("scs_error.data_inventory_supply_exit_61005");
                break;
            }
            $count=MaintenanceStorageItem::where("shop_id",$mil->shop_id)->where("status",1)->where("type",1)->where("data_id",$mil->date_id)->where("current_number",">",0)->where("item_id",Input::get("item_id"))->sum("current_number");
            $current_number=Input::get("number");
            if($count>$current_number){//账面大于 实际盘亏
                $type=1;
            }else if($count==$current_number){//账面等于 实际盘平
                $type=2;
            }else{//账面小于 实际盘盈
                $type=0;
            }
            $mis=new MaintenanceInventorySupply;
            $mis->list_id=$mil->id;
            $mis->shop_id=$mil->shop_id;
            $mis->item_type=Input::get("item_type");
            $mis->item_id=Input::get("item_id");
            $mis->date_number=$count;
            $mis->current_number=$current_number;
            $mis->number=$current_number-$count;
            $mis->check_number=$current_number-$count;
            $mis->status=0;
            $mis->type=$type;
            $mis->cust_id=$mil->cust_id;
            $mis->date_id=$mil->date_id;
            $mis->create_by=$sc_user->id;
            $mis->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$mis->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addSupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-05 09:14:27
     * @Description: 修改一个已经 添加的盘点表记录
     */
    public function editSupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",
                'number'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mis=MaintenanceInventorySupply::where("id",Input::get("id"))->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mis)){
                $response = Lang::get("scs_error.data_inventory_supply_notexit_61006");
                break;
            }
            if($mis->status!=0){//如果该改库存盘点记录已经确认 或者执行
                $response = Lang::get("scs_error.data_inventory_supply_status_error_61007");
                break;
            }
            $count=MaintenanceStorageItem::where("shop_id",$mis->shop_id)->where("status",1)->where("type",1)->where("data_id",$mis->date_id)->where("current_number",">",0)->where("item_id",$mis->item_id)->sum("current_number");
            $current_number=Input::get("number");
            if($count>$current_number){
                $type=1;
            }else if($count==$current_number){
                $type=2;
            }else{
                $type=0;
            }
            $mis->date_number=$count;
            $mis->current_number=$current_number;
            $mis->number=$current_number-$count;
            $mis->check_number=$current_number-$count;
            $mis->type=$type;
            $mis->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$mis->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "editSupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-04 19:16:14
     * @Description: 确认一个库存盘点记录的 和他额盘点表
     */
    public function comfimSupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mil=MaintenanceInventoryList::where("id",Input::get("list_id"))->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mil)){
                $response = Lang::get("scs_error.data_inventory_notexit_61003");
                break;
            }
            if($mil->status!=0){//如果该改库存盘点记录已经确认 或者执行
                $response = Lang::get("scs_error.data_inventory_status_error_61004");
                break;
            }
            $mis=MaintenanceInventorySupply::where("list_id",$mil->id)->where("status",0)->get();
            if(count($mis)<=0){
                $response = Lang::get("scs_error.data_inventory_status_error_61016");
                break;
            }
            $mil->status=1;
            $mil->save();
            MaintenanceInventorySupply::where("list_id",$mil->id)->where("status",0)->update(array("status"=>1));
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$mil->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "comfimSupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-05 11:17:36
     * @Description: 查看没有盘点完成的 盘点表
     * 查看 盘点表的记录 筛选 未完产盘平操作的盘点表记录
     */
    public function getChcekSupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mil=MaintenanceInventoryList::where("id",Input::get("list_id"))->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mil)){
                $response = Lang::get("scs_error.data_inventory_notexit_61003");
                break;
            }
            if($mil->status!=1){
                $response = Lang::get("scs_error.data_inventory_status_error_61008");
                break;
            }
            $search_retrieval_code=Input::get("retrieval_code");
            $level_type=Input::get("level_type",0);
            $level_code=Input::get("level_code",0);
            $car_str=Input::get("car_str");
            $three=SupplyItem::getThreeClassification(Input::get('list_id'));
            // $non_ids=SupplyItemNonStandard::retrievalCodeNew($sc_user->shop_id,$search_retrieval_code,$car_str,$level_type,$level_code,$three);//获得非标的耗材id范围
            if($search_retrieval_code||$car_str||($level_type!=0&&$level_code!=0)||sizeof($three)>0){
                $si_ids=SupplyItem::retrievalCodeNew($search_retrieval_code,$car_str,$level_type,$level_code,$three);//获得标准的耗材id范围
            }
            // $si_ids=SupplyItem::retrievalCodeNew($search_retrieval_code,$level_type,$car_str,$level_code,$three);//获得标准的耗材id范围
            //已经 盘点过的耗材记录
            $si_sql="select si.id,si.type from supply_item si";
            if(isset($si_ids)){
                $si_ids[]=0;
                $si_sql.=" where si.id in (".implode(",",$si_ids).")";
            }
            // $sins_sql="select sins.id,1 type from supply_item_non_standard sins ";
            // if(isset($non_ids)){
            //     $non_ids[]=0;
            //     $sins_sql.=" where  sins.id in (".implode(",",$non_ids).")";
            // }
            $item_id=$non_item_id=array();
            $count_sql="SELECT count(1) count FROM `maintenance_inventory_supply` mis join (
                    ".$si_sql.") temp
                    where mis.list_id=? and mis.status=1 and mis.check_number!=0 and temp.id=mis.item_id ";
            $count=DB::select($count_sql,array($mil->id));
            $sql="SELECT mis.* FROM `maintenance_inventory_supply` mis join (
                    ".$si_sql.") temp
                    where mis.list_id=? and mis.status=1 and mis.check_number!=0  and temp.id=mis.item_id limit ?,?";
                    // print_r($sql);exit;
            $mis=DB::select($sql,array($mil->id,$page_size*($page-1),$page_size));
            foreach ($mis as $_mis) {
                // if($_mis->item_type==0){//说明是标准耗材
                    $item_id[]=$_mis->item_id;
                    $si_info=SupplyItem::getCacheItemInfo($_mis->item_id);
                    $_mis->code=$si_info->code;
                    $_mis->old_code=$si_info->old_code;
                    $_mis->brand_name=$si_info->brand_name;
                    $_mis->series_name=$si_info->series_name;
                    $_mis->model=$si_info->model;
                    $_mis->specification=$si_info->specification;
                    $_mis->scf_id=$si_info->st_classifi_id;
                    $_mis->scf_name=$si_info->scf_name;
                    $_mis->unit=$si_info->unit;
                    $_mis->item_name=$si_info->name;
                // }else{
                //     $non_item_id[]=$_mis->item_id;
                //     $sins_info=SupplyItemNonStandard::getCacheItemInfo($_mis->item_id);
                //     $_mis->code=$sins_info->code;
                //     $_mis->old_code=$sins_info->old_code;
                //     $_mis->brand_name=$sins_info->brand_name;
                //     $_mis->series_name=$sins_info->series_name;
                //     $_mis->model=$sins_info->model;
                //     $_mis->specification=$sins_info->specification;
                //     $_mis->scf_id=$sins_info->st_classifi_id;
                //     $_mis->scf_name=$sins_info->scf_name;
                //     $_mis->unit=$sins_info->unit;
                // }
                $sc=SCUser::find($_mis->create_by);
                $_mis->cust_name=$sc->real_name;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count[0]->count,"check_info"=>$mis));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getChcekSupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-05 13:41:14
     * @Description: 根据盘点表获取 该盘点表的耗材的入库批次
     */
    public function getStorage(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mis=MaintenanceInventorySupply::where("id",Input::get("id"))->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mis)){
                $response = Lang::get("scs_error.data_inventory_supply_notexit_61006");
                break;
            }
            if($mis->status!=1){//说明该盘点表还未确认
                $response = Lang::get("scs_error.data_inventory_supply_status_error_61009");
                break;
            }
            //获取耗材信息
            // if($mis->item_type==1){
            //    $item_info=SupplyItemNonStandard::getCacheItemInfo($mis->item_id);
            // }else{
            $item_info=SupplyItem::getCacheItemInfo($mis->item_id);
            $item_info->item_name=$item_info->name;
            // }

            $filit_id=MaintenanceInventoryStorage::where("mis_id",$mis->id)->select("storage_id")->get()->toArray();//已经 生成  耗材批次盈亏表的 批次

            $msi_list=MaintenanceStorageItem::leftJoin("maintenance_storage_list","maintenance_storage_list.id","=","maintenance_storage_item.list_id")->leftJoin("maintenance_supplier","maintenance_supplier.id","=","maintenance_storage_list.supplier")
                ->where("maintenance_storage_item.shop_id",$mis->shop_id)
                ->where("maintenance_storage_item.status",1)
                ->where("maintenance_storage_item.type",1)
                ->where("maintenance_storage_item.data_id",$mis->date_id)
                // ->where("maintenance_storage_item.item_type",$mis->item_type)
                ->where("maintenance_storage_item.item_id",$mis->item_id)
                ->whereNotIn("maintenance_storage_item.id",$filit_id)
                ->orderBy("maintenance_storage_item.created_at","desc")
                ->select("maintenance_storage_item.id","maintenance_storage_item.list_id","maintenance_storage_item.item_type","maintenance_storage_item.item_id","maintenance_storage_item.location","maintenance_storage_item.location","maintenance_storage_item.current_number","maintenance_storage_item.number","maintenance_storage_item.tax_purchase_price","maintenance_storage_item.no_tax_purchase_price","maintenance_supplier.name");
            $count=$msi_list->count();
            $msi_list=$msi_list->skip($page_size*($page-1))->take($page_size)->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"msi_info"=>$msi_list,"item_info"=>$item_info));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-05 15:36:22
     * @Description: 盘点耗材的批次记录
     */
    public function addStorageChcek(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'storage_id'=>"required",//耗材批次id
                'id'=>"required",//盘点表id
                'number'=>"required|min:0",//盘点数量
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mis=MaintenanceInventorySupply::where("id",Input::get("id"))->where("shop_id",$sc_user->shop_id)->first();//查看耗材的盘点表是是否存在
            if(empty($mis)){
                $response = Lang::get("scs_error.data_inventory_supply_notexit_61006");
                break;
            }
            if($mis->status!=1){//说明该盘点表还未确认
                $response = Lang::get("scs_error.data_inventory_supply_status_error_61009");
                break;
            }

            $mil=MaintenanceInventoryList::where("id",$mis->list_id)->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mil)){
                $response = Lang::get("scs_error.data_inventory_notexit_61003");
                break;
            }
            if($mil->status!=1){//如果库存盘点记录不是 确认状态  说明是执行完成或者删除 新建状态
                $response = Lang::get("scs_error.data_inventory_status_error_61008");
                break;
            }

            $msi=MaintenanceStorageItem::where("id",Input::get("storage_id"))->where("shop_id",$mis->shop_id)->where("status",1)
                ->where("type",1)->first();//查看所选择的耗材批次  是否正确
            if(empty($msi)){
                $response = Lang::get("scs_error.data_inventory_storage_status_error_61010");
                break;
            }
            $miso=MaintenanceInventoryStorage::where("mis_id",$mis->id)->where("storage_id",$msi->id)->first();
            if($miso){
                $response = Lang::get("scs_error.data_inventory_storage_exit_error_61011");
                break;
            }
            if(Input::get("number")==$msi->current_number){//如果盘点数量等于当前的数量  说明是 没有差异
                $response = Lang::get("scs_error.data_inventory_storage_number_right_61013");
                break;
            }
            if($msi->item_id!=$mis->item_id){//判断盘点耗材与所选批次中耗材是否一致
                $response = Lang::get("scs_error.data_inventory_storage_item_error_61021");
                break;
            }
            // if(Input::get("number")>$msi->number){//如果盘点数量大于 入库的数量 报错
            //     $response = Lang::get("scs_error.data_inventory_storage_number_error_61012");
            //     break;
            // }
            $current_number=Input::get("number");
            $temp_number=$current_number-$msi->current_number;//差异数等于 实盘数 减去 账面数


            $miso=new MaintenanceInventoryStorage;
            $miso->mis_id=$mis->id;
            $miso->storage_id=$msi->id;
            $miso->storage_list_id=$msi->list_id;
            $miso->list_id=$mis->list_id;//库存 分类
            $miso->shop_id=$mis->shop_id;
            $miso->item_type=$mis->item_type;
            $miso->item_id=$mis->item_id;
            $miso->date_number=$msi->current_number;
            $miso->current_number=$current_number;
            $miso->number=$temp_number;
            $miso->status=0;
            $miso->cust_id=$mis->cust_id;
            $miso->create_by=$sc_user->id;
            $miso->save();

            $mis->check_number=$mis->check_number-$temp_number;// 盘点表的 盘点差异数 减去 处理差异数等于 剩下差异数
            $mis->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$miso->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addStorageChcek", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-05 18:46:04
     * @Description: 库存盘点记录的 耗材批次盘点记录
     */
    public function getStorageChcek(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mil=MaintenanceInventoryList::where("id",Input::get("list_id"))->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mil)){
                $response = Lang::get("scs_error.data_inventory_notexit_61003");
                break;
            }
            $search_retrieval_code=Input::get("retrieval_code");
            $level_type=Input::get("level_type",0);
            $level_code=Input::get("level_code",0);
            $three=SupplyItem::getThreeClassification(Input::get('list_id'));;
            $car_str=Input::get("car_str");
            // $non_ids=SupplyItemNonStandard::retrievalCodeNew($sc_user->shop_id,$search_retrieval_code,$car_str,$level_type,$level_code,$three);//获得非标的耗材id范围
            // $si_ids=SupplyItem::retrievalCodeNew($search_retrieval_code,$level_type,$car_str,$level_code,$three);//获得标准的耗材id范围
            if($search_retrieval_code||$car_str||($level_type!=0&&$level_code!=0)||sizeof($three)>0){
                $si_ids=SupplyItem::retrievalCodeNew($search_retrieval_code,$car_str,$level_type,$level_code,$three);//获得标准的耗材id范围
            }
            //已经 盘点过的耗材记录
            $si_sql="select si.id,si.type from supply_item si";
            if(isset($si_ids)){
                $si_ids[]=0;
                $si_sql.=" where si.id in (".implode(",",$si_ids).")";
            }
            // $sins_sql="select sins.id,1 type from supply_item_non_standard sins ";
            // if(isset($non_ids)){
            //     $non_ids[]=0;
            //     $sins_sql.=" where  sins.id in (".implode(",",$non_ids).")";
            // }
            $item_id=$non_item_id=array();
            $count_sql="SELECT count(1) count FROM `maintenance_inventory_storage` mis join (
                    ".$si_sql.") temp
                    where mis.list_id=? and temp.id=mis.item_id ";
            $count=DB::select($count_sql,array($mil->id));
            $sql="SELECT mis.* FROM `maintenance_inventory_storage` mis join (
                    ".$si_sql.") temp
                    where mis.list_id=? and temp.id=mis.item_id limit ?,?";
                    // print_r($sql);exit;
            $mis=DB::select($sql,array($mil->id,$page_size*($page-1),$page_size));
            $class_one_info = array();
            foreach ($mis as $_mis) {
                // if($_mis->item_type==0){//说明是标准耗材
                    $item_id[]=$_mis->item_id;
                    $si_info=SupplyItem::getCacheItemInfo($_mis->item_id);
                    $scf=SupplyClassification::getClassification($si_info->st_classifi_id);
                    $_mis->code=$si_info->code;
                    $_mis->old_code=$si_info->old_code;
                    $_mis->brand_name=$si_info->brand_name;
                    $_mis->series_name=$si_info->series_name;
                    $_mis->model=$si_info->model;
                    $_mis->specification=$si_info->specification;
                    $_mis->scf_id=$si_info->st_classifi_id;
                    $_mis->scf_name=$si_info->scf_name;
                    $_mis->unit=$si_info->unit;
                    $_mis->two_id=$scf->two_id;
                    $_mis->two=$scf->two;
                    $_mis->one_id=$scf->one_id;
                    $_mis->one=$scf->one;
                    $_mis->car_model_comments=$si_info->car_model_comments;
                    $_mis->item_name=$si_info->name;
                // }else{
                //     $non_item_id[]=$_mis->item_id;
                //     $sins_info=SupplyItemNonStandard::getCacheItemInfo($_mis->item_id);
                //     $scf=StatisticalClassification::getClassification($sins_info->st_classifi_id);
                //     $_mis->code=$sins_info->code;
                //     $_mis->old_code=$sins_info->old_code;
                //     $_mis->brand_name=$sins_info->brand_name;
                //     $_mis->series_name=$sins_info->series_name;
                //     $_mis->model=$sins_info->model;
                //     $_mis->specification=$sins_info->specification;
                //     $_mis->scf_id=$sins_info->st_classifi_id;
                //     $_mis->scf_name=$sins_info->scf_name;
                //     $_mis->unit=$sins_info->unit;
                //     $_mis->two_id=$scf->two_id;
                //     $_mis->two=$scf->two;
                //     $_mis->one_id=$scf->one_id;
                //     $_mis->one=$scf->one;
                //     $_mis->car_model=$sins_info->car_model;
                //     $_mis->car_model_comments=$sins_info->car_model;
                // }
                $temp_msi=MaintenanceStorageItem::find($_mis->storage_id);
                $amount=$temp_msi->tax_purchase_price*$_mis->number;
                $sc=SCUser::find($_mis->create_by);
                $_mis->cust_name=$sc->real_name;
                $_mis->amount=$amount;
                if(!isset($class_one_info[$_mis->one_id])){ //如果没有一类ID，创建。如果有则相加
                    $class_one_info[$_mis->one_id]['one_id'] = $_mis->one_id;
                    $class_one_info[$_mis->one_id]['name'] = $_mis->one;
                    $class_one_info[$_mis->one_id]['number'] = $_mis->number;
                    $class_one_info[$_mis->one_id]['tax_purchase_subtotal'] = bcdiv($_mis->amount,100,6);
                } else {
                    $class_one_info[$_mis->one_id]['number'] += $_mis->number;
                    $class_one_info[$_mis->one_id]['tax_purchase_subtotal'] += bcdiv($_mis->amount,100,6);
                }
            }
            $total_amount=DB::select("SELECT sum(mis.number*msi.tax_purchase_price) amount FROM `maintenance_inventory_storage` mis  LEFT JOIN maintenance_storage_item msi on msi.id=mis.storage_id where mis.list_id=".$mil->id);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count[0]->count,"total_amount"=>$total_amount[0]->amount,"supply_storage"=>$mis,"class_one_info"=>array_values($class_one_info)));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getStorageChcek", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-05 15:36:22
     * @Description: 删除 耗材批次盘点记录
     */
    public function deleteStorageChcek(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",//盘点表id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $miso=MaintenanceInventoryStorage::where("id",Input::get("id"))->where("shop_id",$sc_user->shop_id)->first();//查看耗材批次的盘点记录是否存
            if(empty($miso)){
                $response = Lang::get("scs_error.data_inventory_supply_notexit_61006");
                break;
            }
            if($miso->status!=0){//说明是 新建还未执行的 批次记录
                $response = Lang::get("scs_error.data_inventory_supply_status_error_61009");
                break;
            }
            $mis=MaintenanceInventorySupply::find($miso->mis_id);
            $mis->check_number=$mis->check_number+$miso->number;
            $mis->save();
            $miso->delete();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$mis->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deleteStorageChcek", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-05-06 09:47:47
     * @Description: 执行一个库存盘点
     */
    public function implementInventory(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mil=MaintenanceInventoryList::where("id",Input::get("list_id"))->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mil)){
                $response = Lang::get("scs_error.data_inventory_notexit_61003");
                break;
            }
            if($mil->status!=1){
                $response = Lang::get("scs_error.data_inventory_status_error_61008");
                break;
            }
            //检查所有的 盘点表是否都已盘平
            $mis=MaintenanceInventorySupply::where("list_id",$mil->id)->where("check_number","!=",0)->first();//如果存在还未盘平的 盘点表记录
            if($mis){
                $response = Lang::get("scs_error.data_inventory_number_error_61014");
                break;
            }
            $response=MaintenanceInventoryList::implement($mil->id,$sc_user->id);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "implementInventory", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-06 11:30:21
     * @Description: 删除一个 库存盘点记录
     */
    public function deleteInventory(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mil=MaintenanceInventoryList::where("id",Input::get("list_id"))->where("shop_id",$sc_user->shop_id)->first();
            if(empty($mil)){
                $response = Lang::get("scs_error.data_inventory_notexit_61003");
                break;
            }
            if(in_array($mil->status,array(2,3))){
                $response = Lang::get("scs_error.data_inventory_status_error_61015");
                break;
            }
            $mil->status=3;
            $mil->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$mil->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deleteInventory", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-5-6 16:31:51
     * @Description: 盘点详情
     */
    public function inventoryDetail(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msl=MaintenanceInventoryList::where('maintenance_inventory_list.id',Input::get('id'))
                ->leftJoin("sc_user",'sc_user.id','=','maintenance_inventory_list.cust_id')
                ->leftJoin("maintenance_datebase",'maintenance_datebase.id','=','maintenance_inventory_list.date_id')
                ->where("maintenance_inventory_list.shop_id",$sc_user->shop_id)
                ->select("maintenance_inventory_list.*","maintenance_datebase.name as data_name","sc_user.real_name")
                ->first();
            if($msl->implement_time){
                $msl->implement_time=date("Y-m-d H:i:s",strtotime($msl->implement_time)+Config::get("config.report_time_diff"));
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("inventory_info"=>$msl));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "detail", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-10 15:25:16
     * @Description: 获取新的三级分类和添加的盘点包
     */
    public function st(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $one=$two=$three=array();
            //获取一级分类
            $one_one=InventoryClassification::leftJoin('supply_classification','supply_classification.id','=','inventory_classification.scf_id')
                ->where('inventory_classification.list_id',Input::get('list_id'))
                ->where('inventory_classification.level',1)
                ->select('supply_classification.id','supply_classification.name')
                ->distinct('sf1.id')
                ->get()
                ->toArray();
            $one_two=InventoryClassification::leftJoin('supply_classification as sf2','sf2.id','=','inventory_classification.scf_id')
                ->leftJoin('supply_classification as sf1','sf1.id','=','sf2.parent_id')
                ->where('inventory_classification.list_id',Input::get('list_id'))
                ->where('inventory_classification.level',2)
                ->select('sf1.id','sf1.name')
                ->distinct('sf1.id')
                ->get()
                ->toArray();
            $one_three=InventoryClassification::leftJoin('supply_classification as sf3','sf3.id','=','inventory_classification.scf_id')
                ->leftJoin('supply_classification as sf2','sf2.id','=','sf3.parent_id')
                ->leftJoin('supply_classification as sf1','sf1.id','=','sf2.parent_id')
                ->where('inventory_classification.list_id',Input::get('list_id'))
                ->where('inventory_classification.level',3)
                ->distinct('sf1.id')
                ->select('sf1.id','sf1.name')
                ->get()
                ->toArray();
            
            foreach($one_one as $_one_one){
                $one[]=array("id"=>$_one_one['id'],"name"=>$_one_one['name']);
            }
            foreach($one_two as $_one_two){
                $one[]=array("id"=>$_one_two['id'],"name"=>$_one_two['name']);
            }
            foreach($one_three as $_one_three){
                $one[]=array("id"=>$_one_three['id'],"name"=>$_one_three['name']);
            }
            
            //获取二级分类
            $scf_one=InventoryClassification::where('list_id',Input::get('list_id'))->where('level',1)->select('scf_id')->get()->toArray();
            $scf_one=array_column($scf_one,'scf_id');
            
            $one_two=SupplyClassification::where('level',2)->whereIn('parent_id',$scf_one)->select('id','name')->get()->toArray();
            $two_two=InventoryClassification::leftJoin('supply_classification as sf2','sf2.id','=','inventory_classification.scf_id')
            ->where('inventory_classification.list_id',Input::get('list_id'))
            ->where('inventory_classification.level',2)
            ->select('sf2.id','sf2.name')
            ->distinct('sf2.id')
            ->get()
            ->toArray();
            $three_two=InventoryClassification::leftJoin('supply_classification as sf3','sf3.id','=','inventory_classification.scf_id')
                ->leftJoin('supply_classification as sf2','sf2.id','=','sf3.parent_id')
                ->where('inventory_classification.list_id',Input::get('list_id'))
                ->where('inventory_classification.level',3)
                ->select('sf2.id','sf2.name')
                ->distinct('sf2.id')
                ->get()
                ->toArray();
            foreach($one_two as $_one_two){
                $two[]=array("id"=>$_one_two['id'],"name"=>$_one_two['name']);
            }
            foreach($two_two as $_two_two){
                $two[]=array("id"=>$_two_two['id'],"name"=>$_two_two['name']);
            }
            foreach($three_two as $_three_two){
                $two[]=array("id"=>$_three_two['id'],"name"=>$_three_two['name']);
            }
            
            //获取三级分类
            //根据第一分类获取三级分类
            $scf_one=InventoryClassification::where('list_id',Input::get('list_id'))->where('level',1)->select('scf_id')->get()->toArray();
            $scf_one=array_column($scf_one,'scf_id');
            $one_two=SupplyClassification::where('level',2)->whereIn('parent_id',$scf_one)->select('id')->get()->toArray();
            $scf_two=array_column($one_two,'id');
            $one_three=SupplyClassification::where('level',3)->whereIn('parent_id',$scf_two)->select('id','name')->get()->toArray();
            //根据第二份额里获取三级分类
            $scf_two=InventoryClassification::where('list_id',Input::get('list_id'))->where('level',2)->select('scf_id')->get()->toArray();
            $scf_two=array_column($scf_two,'scf_id');
            $two_three=SupplyClassification::where('level',3)->whereIn('parent_id',$scf_two)->select('id','name')->get()->toArray();
            //根据第二份额里获取三级分类
            $three_three=InventoryClassification::leftJoin('supply_classification as sf3','sf3.id','=','inventory_classification.scf_id')
            ->where('inventory_classification.list_id',Input::get('list_id'))
            ->where('inventory_classification.level',3)
            ->select('sf3.id','sf3.name')
            ->get()
            ->toArray();
            foreach($one_three as $_one_three){
                $three[]=array("id"=>$_one_three['id'],"name"=>$_one_three['name']);
            }
            foreach($two_three as $_two_three){
                $three[]=array("id"=>$_two_three['id'],"name"=>$_two_three['name']);
            }
            foreach($three_three as $_three_three){
                $three[]=array("id"=>$_three_three['id'],"name"=>$_three_three['name']);
            }
            //获取添加的盘点包
            $ilc=InventoryListCondition::leftJoin('inventory_condition','inventory_condition.id','=','inventory_list_condition.ic_id')
                ->where('list_id',Input::get('list_id'))
                ->select('inventory_condition.id','inventory_condition.name')
                ->get();
            if(sizeof($one)==0 && sizeof($two)==0 && sizeof($three)==0){
                $one=$two=$three=array();
                $st=SupplyClassification::where("level",1)->where("status",1)->select('id','name')->get()->toArray();
                $one=$st;
                foreach ($st as $_st) {
                    $temp_st=SupplyClassification::where("parent_id",$_st['id'])->where("status",1)->select('id','name')->get()->toArray();
                    $two=array_merge($two,$temp_st);
                    foreach ($temp_st as $_temp_st) {
                        $temp_st2=SupplyClassification::where("parent_id",$_temp_st['id'])->where("status",1)->select('id','name')->get()->toArray();
                        $three=array_merge($three,$temp_st2);
                    }
                }
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("one"=>$one,'two'=>$two,'three'=>$three,'ilc'=>$ilc));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "st", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}